package com.myteam.mystory.DAO;

import java.util.ArrayList;

import com.myteam.mystory.entity.CategoryEntity;
import com.myteam.mystory.entity.ChapterEntity;
import com.myteam.mystory.utils.LogUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ChapterDAO {
	public static final String TAG = "CategoriesDAO";
	private final SQLiteDatabase db;
	
	public ChapterDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_chapter(ChapterEntity chapterEntity){
		ContentValues value = new ContentValues();
		value.put(ChapterScheme.STORYID, chapterEntity.getStoryId());
		value.put(ChapterScheme.CHAPTERNAME, chapterEntity.getChapterName());
		value.put(ChapterScheme.CHAPTERCONTENT, chapterEntity.getChapterContent());
		value.put(ChapterScheme.IMGLINK, chapterEntity.getImgLink());
		value.put(ChapterScheme.SYNCDATE, chapterEntity.getSyncDate());
		value.put(ChapterScheme.HISTORY, chapterEntity.getHistory());
		value.put(ChapterScheme.BOOKMARK, chapterEntity.getBookmark());
		value.put(ChapterScheme.STATUT, chapterEntity.getStatut());
		if(db.insert(ChapterScheme.TABLE_CHAPTER_NAME, null, value)==-1){
			LogUtils.e("Bug insert chapter");
		} else {
			LogUtils.e("successfully insert chapter");
		}
		return 0;
	}
	
	public ArrayList<ChapterEntity> getAllChapter() {
		ArrayList<ChapterEntity> list_chapter = new ArrayList<ChapterEntity>();
		String query = "select * from " + ChapterScheme.TABLE_CHAPTER_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()){
			do {
				ChapterEntity chapterEntity = new ChapterEntity();
				chapterEntity.setStoryId(cursor.getString(0));
				chapterEntity.setChapterName(cursor.getString(1));
				chapterEntity.setChapterContent(cursor.getString(2));
				chapterEntity.setImgLink(cursor.getString(3));
				chapterEntity.setSyncDate(cursor.getString(4));
				chapterEntity.setHistory(cursor.getInt(5));
				chapterEntity.setBookmark(cursor.getInt(6));
				chapterEntity.setStatut(cursor.getString(7));
				list_chapter.add(chapterEntity);
			} while (cursor.moveToNext());
		}
		return list_chapter;
	}
	
	public long deletetablechapter() {
		long result = 0;
		try {
			result = db.delete(ChapterScheme.TABLE_CHAPTER_NAME, null, null);
			LogUtils.e("delete table chapter successfully");
		} catch (Exception e) {
			LogUtils.e("delete table chapter fail");
		}
		return result;
	}
}
